﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace DataAccessObject.Cache
{
    public class RuntimeCache
    {

        #region 删除缓存
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        public void Delete(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }
        public void Delete(string[] CacheKeys)
        {
            foreach (var key in CacheKeys)
            {
                HttpRuntime.Cache.Remove(key);
            }
        }
        #endregion

        #region 获取缓存，依赖时间
        /// <summary>
        /// 获取缓存，依赖时间
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <returns></returns>
        public T Get<T>(string CacheKey)
        {
            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
            object obj_cache = HttpRuntime.Cache[CacheKey];
            if (obj_time != null && obj_cache != null)
            {
                if (Convert.ToDateTime(obj_time) < DateTime.Now)
                {
                    Delete(CacheKey);
                    Delete(CacheKey + "_time");
                    return default(T);
                }
                else return (T)obj_cache;
            }
            else
            {
                Delete(CacheKey);
                Delete(CacheKey + "_time");
                return default(T);
            }
        }
        #endregion

        #region 获取缓存，依赖文件
        /// <summary>
        /// 获取缓存，依赖文件
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="depFile">依赖的文件</param>
        /// <returns></returns>
        public T Get<T>(string CacheKey, string depFile)
        {
            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];
            object obj_cache = HttpRuntime.Cache[CacheKey];
            if (File.Exists(depFile))
            {
                FileInfo fi = new FileInfo(depFile);

                if (obj_time != null && obj_cache != null)
                {
                    if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)
                    {
                        Delete(CacheKey);
                        Delete(CacheKey + "_time");
                        return default(T);
                    }
                    else return (T)obj_cache;
                }
                else
                {
                    Delete(CacheKey);
                    Delete(CacheKey + "_time");
                    return default(T);
                }
            }
            else
            {
                throw new Exception("文件(" + depFile + ")不存在！");
            }
        }
        #endregion

        #region 简单的插入缓存
        /// <summary>
        /// 简单的插入缓存
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="objObject">数据</param>
        public bool Set<T>(string CacheKey, T objObject)
        {
            HttpRuntime.Cache.Insert(CacheKey, objObject);
            return true;
        }
        #endregion

        #region 有过期时间的插入缓存数据
        public bool Set<T>(string CacheKey, T objObject, DateTime expiresAt)
        {
            HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, System.Web.Caching.Cache.NoSlidingExpiration);
            HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, System.Web.Caching.Cache.NoSlidingExpiration);//存储过期时间
            return true;
        }
        #endregion

        #region 插入缓存数据，指定缓存多少秒
        public bool Set<T>(string CacheKey, T objObject, int seconds)
        {
            DateTime expiresAt = DateTime.Now.AddSeconds(seconds);
            HttpRuntime.Cache.Insert(CacheKey, objObject, null, expiresAt, System.Web.Caching.Cache.NoSlidingExpiration);
            HttpRuntime.Cache.Insert(CacheKey + "_time", expiresAt, null, expiresAt, System.Web.Caching.Cache.NoSlidingExpiration);//存储过期时间
            return true;
        }
        #endregion
    }
}